/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class CourseSearchUtils {
	
	private static final String REGEX_DEPARTMENT = "[a-zA-Z]{2,4}";
	private static final String REGEX_COURSE = "[0-9]{3}[a-zA-Z]?";
	private static final String REGEX_SECTION = "[a-zA-Z0-9]{3}";
	private static final String REGEX_KEYWORDS = ".{5,}+";
	
	private static final String REGEX_LETTERS = "[a-zA-Z]";
	private static final String REGEX_NUMBERS = "[0-9]";
	private static final String REGEX_ALPHANUMERIC = "[a-zA-Z0-9]*";
	
	//Change URL encoded spaces into String spaces and force uppercase
	public static String parseSearch(String in) {
		if(in == null)
			return "";
		String out = "";
		try {
			out = URLDecoder.decode(in, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		out = out.trim();
		out = out.toUpperCase();
		return out;
	}
	
	//Convert string search to array format
	public static String[] toWordArray(String in){
		String[] out = new String[1];
		out[0] = in;
		if (in.contains(" "))
			out = in.split(" ");
		else if(findSplitLocation(in) >= 0) {
			out = new String[2];
			out[0] = in.substring(0,findSplitLocation(in));
			out[1] = in.substring(findSplitLocation(in));
		}
		return out;
	}
	
	//Count number of terms in the search string
	public static int countTerms(String in){
		if(in.equals(""))
			return 0;
		if(in.contains(" "))
			return in.split(" ").length;
		else
			return 1;
	}
	
	//finds split location; when a letter is followed by a number
	private static int findSplitLocation(String in){
		if(in.matches(REGEX_ALPHANUMERIC)){
			if((in.charAt(0)+"").matches(REGEX_LETTERS)){
				for(int i = 0; i < in.length(); i++){
					if((in.charAt(i)+"").matches(REGEX_NUMBERS)){
						return i;
					}
				}
			}
			else{
				for(int i = 0; i < in.length(); i++){
					if((in.charAt(i)+"").matches(REGEX_LETTERS)){
						return i;
					}
				}
			}
		}
			return -1;
	}
	
	public static String findSection(String[] in) {
		for(int i = 0; i < in.length; i++){
			if(in[i].matches(REGEX_SECTION) && !in[i].equals(findCourse(in)))
				return in[i];
		}
		return "";
	}
	
	//heuristic to find the course in the list of words
	public static String findCourse(String[] in){
		for(int i = 0; i < in.length; i++){
			if(in[i].matches(REGEX_COURSE))
				return in[i];
		}
		return "";
	}
	
	//heuristic to find the department in the list of words
	public static String findDepartment(String[] in){
		for(int i = 0; i < in.length; i++){
			if(in[i].matches(REGEX_DEPARTMENT))
				return in[i];
		}
		return "";
	}
	
	public static List<String> findSearchTerms(String[] in){
		List<String> out = new ArrayList<String>();
		for(int i = 0; i < in.length; i++){
			if(in[i].matches(REGEX_KEYWORDS))
				out.add(in[i]);
		}
		return out;
	}
	
	public static void main(String[] args){
		String test = "cpsc 410 001";
//		String split = test.substring(0,findSplitLocation(test));
//		String splat = test.substring(findSplitLocation(test));
//		System.out.println("Split "+ split);
//		System.out.println("Splat "+ splat);
		test = parseSearch(test);
		System.out.println("Parsed: "+test);
		String[] split = toWordArray(test);
		String dept = findDepartment(split);
		String course = findCourse(split);
		String section = findSection(split);
		System.out.println("Dept: "+dept+" Course: "+course+" Section: "+section);
	}

}
